package com.esirong.greendao;


import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Property;
import de.greenrobot.daogenerator.Schema;
import de.greenrobot.daogenerator.ToMany;

/**
 *
 * @author esirong
 *
 */
public class DaoGenerator {

    public static String daoPackage = "com.esirong.dao";

    public static void main(String[] args) throws Exception {
        Schema schema = new Schema(5, daoPackage);
        addNote(schema);
        addTask(schema);
        addGoal(schema);
        addLabel(schema);
        addAddress(schema);
        new de.greenrobot.daogenerator.DaoGenerator().generateAll(schema, args[0]);
    }

    /**
     * 成生注解表
     * @param schema
     */
    private static void addNote(Schema schema) {
        Entity note = schema.addEntity("Note");
        note.addIdProperty();
        note.addStringProperty("text").notNull();
        note.addDateProperty("date");
    }

    /**
     * 生成任务表
     * @param schema
     */
    private static void addTask(Schema schema){
        //任务开始时，结束时，提醒时
        Entity task = schema.addEntity("Task");
        task.addIdProperty();
        task.addStringProperty("title");
        task.addStringProperty("note");
        task.addStringProperty("type");
        task.addIntProperty("priority");
        task.addLongProperty("start_at");
        task.addLongProperty("end_at");
        task.addLongProperty("alert_at");
        task.addIntProperty("status");
        task.addBooleanProperty("finished");
        task.addBooleanProperty("done");
        task.addIntProperty("score");
        task.addStringProperty("address");

        //标签
        Entity label = schema.addEntity("Label");
        label.addIdProperty();
        label.addStringProperty("name");
        label.addIntProperty("pos");

        Property taskiId = label.addLongProperty("taskId").notNull().getProperty();
        label.addToOne(task, taskiId);

        ToMany taskToLabels = task.addToMany(label, taskiId);
        taskToLabels.setName("Label");

        //创建联合表， 以实现多对多表关系
        addTaskLabel(schema);
        addTaskGoal(schema);


    }

    private static Entity addTaskLabel(Schema schema) {
        Entity taskLabel = schema.addEntity("Task_Label");
        taskLabel.addIdProperty();
        Property taskiId = taskLabel.addLongProperty("taskId").notNull().getProperty();
        Property labelId = taskLabel.addLongProperty("labelId").notNull().getProperty();
        return taskLabel;
    }
    private static Entity addTaskGoal(Schema schema) {
        Entity taskLabel = schema.addEntity("Task_Goal");
        taskLabel.addIdProperty();
        Property taskiId = taskLabel.addLongProperty("taskId").notNull().getProperty();
        Property labelId = taskLabel.addLongProperty("goalId").notNull().getProperty();
        return taskLabel;
    }

    private static void addLabel(Schema schema){

    }
    private static void addGoal(Schema schema){
        Entity goal = schema.addEntity("Goal");
        goal.addIdProperty();
        goal.addStringProperty("name");
        goal.addIntProperty("pos");

    }
    //地点
    private static void addAddress(Schema schema){
        Entity address = schema.addEntity("Address");
        address.addIdProperty();
        address.addStringProperty("name");
        address.addIntProperty("pos");

    }
}
